package com.rzz.stack_queue;

import java.util.Stack;

/**
 * @description: 用一个栈逆序另一个栈
 * @author: ruanzhizhi
 * @date: 2021/6/6 22:49
 */
public class StackReverseStack {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(5);
        stack.push(4);
        stack.push(3);
        stack.push(2);
        stack.push(1);
        reverse(stack);
    }

    public static void reverse(Stack<Integer> stack) {
        Stack<Integer> newStack = new Stack<>();
        Stack<Integer> newStack1 = stack;

        while (!stack.isEmpty()) {
            Integer pop = stack.pop();
            while (!newStack.isEmpty() && newStack.peek() > pop) {
                stack.push(pop);
            }
            newStack.push(pop);
        }
        while (!newStack.isEmpty()){
            stack.push(newStack.pop());
        }
    }
}
